$.fn.extend({
    createPagination: function(settings) {
        return this.each(function() {
            var createDom = $(this);
            var defaults = {
                pageNumber: 1,
                pageSize: 5,
                pageTotal: 1,
            };
            var options = $.extend({}, defaults, settings);
            var createTemplate = function(currentPage) {
                currentPage = $.toNumber(currentPage);
                var pageTotal = options.pageTotal;
                var pageSize = options.pageSize;
                var pageCycle = Math.ceil(currentPage / pageSize);
                var pageStart = (pageCycle - 1) * pageSize + 1;
                var pageEnd = pageCycle * pageSize;
                if (pageEnd > pageTotal) pageEnd = pageTotal;
                var template = '<ul class="pagination mt0">';
                if (currentPage != 1) template += '   <li><a href="javascript:void(0)" type="prev">&laquo;</a></li>';
                if (pageCycle != 1) template +=
                    '   <li><a href="javascript:void(0)" value="1">1</a></li>' +
                    '   <li><span>...</span></li>';
                for (var i = pageStart; i <= pageEnd; i++)
                    if (i == currentPage) template += '  <li class="active"><span value="' + i + '">' + i + '</span></li>';
                    else template += '  <li><a href="javascript:void(0)" type="page" value="' + i + '">' + i + '</a></li>';
                if (pageCycle != Math.ceil(pageTotal / pageSize)) template +=
                    '   <li><span>...</span></li>' +
                    '   <li><a href="javascript:void(0)" value="' + pageTotal + '">' + pageTotal + '</a></li>';
                if (currentPage != pageTotal) template += '   <li><a href="javascript:void(0)" type="next">&raquo;</a></li>';
                template += '</ul>';
                createDom.empty().createTemplate(template);
                createDom.data('data', {
                    pageTotal: pageTotal,
                    pageSize: pageSize,
                    currentPage: currentPage,
                });
                createDom.find('a').off('click')
                createDom.find('a').on('click', function() {
                    var thisDom = $(this);
                    var type = thisDom.attr('type');
                    var data = createDom.data().data;
                    var pageTotal = data.pageTotal;
                    var pageSize = data.pageSize;
                    var currentPage = data.currentPage;
                    var targetPage = thisDom.attr('value');
                    if (type == 'prev') currentPage - 1 < 1 ? targetPage = 1 : targetPage = currentPage - 1;
                    if (type == 'next') currentPage + 1 > pageTotal ? targetPage = pageTotal : targetPage = currentPage + 1;
                    createDom.trigger('changeCallback', [currentPage, targetPage]);
                    createTemplate(targetPage);
                });
            }
            createTemplate(options.pageNumber);
        });
    },
    createNavList: function(navList) {
        return this.each(function() {
            var createDom = $(this);
            createDom.empty();
            for (var i = 0; i < navList.length; i++) {
                var navItem = navList[i];
                if (navItem.isHide) continue;
                createDom.append($('<a>').addClass('list-group-item').attr('href', navItem.href).data('data', navItem)
                    .append($('<div>').addClass('nav-icon').append($('<i>').addClass(navItem.icon)))
                    .append($('<div>').addClass('nav-title').append($('<h3>').text(navItem.title)).append($('<p>').text(navItem.englishTitle))));

                var currentUrl = window.location.href;
                if (currentUrl.indexOf(navItem.href) != -1) createDom.find('a:last').addClass('active');
            }
        });
    },
    verifyErrorAndHelp: function(message) {
        return this.each(function() {
            var thisDom = $(this);
            thisDom.findFormUnit().verifyError().verifyHelp(message);
        });
    },
    verifyIconErrorAndHelp: function(message) {
        return this.each(function() {
            var thisDom = $(this);
            var thisDomRequired = thisDom[0].id + '-required';
            $(`#${thisDomRequired}`).html(`<span style="color: #a94442">${message}</span>`);
        });
    },
    upload: function(callback) {
        return this.each(function() {
            callback = callback || new Function;
            var thisDom = $(this);
            var files = thisDom.prop('files');
            var params = new FormData();
            params.append('uploadFile', files[0]);
            $.post(_apiPath + '/Elancos/uploadAttachments', params, {}, {
                    processData: false,
                    contentType: false,
                })
                .then(function(result) {
                    callback(result.files.uploadFile[0])
                });
        });
    },
    reloadOptionList: function(optionList, firstOption) {
        return this.each(function() {
            var thisDom = $(this);
            var data = thisDom.data();
            var selectDom = thisDom.findFormUnit();
            if (thisDom.find('.component-select-other-select').length != 0) {
                selectDom = thisDom.find('.component-select-other-select').findFormUnit();
                data = thisDom.find('.component-select-other-select').data();
            }
            if ($.isEmpty(firstOption)) firstOption = data.firstOption;
            selectDom.empty().createOptionList(optionList);
        });
    },
    reloadSelectedOption: function(value) {
        return this.each(function() {
            var thisDom = $(this);
            var data = thisDom.data();
            var selectDom = thisDom.findFormUnit();
            if (thisDom.find('.component-select-other-select').length != 0) {
                selectDom = thisDom.find('.component-select-other-select').findFormUnit();
                data = thisDom.find('.component-select-other-select').data();
            }
            if ($.isEmpty(value)) {
                var domStorage = data.domStorage;
                var fieldValue = data.fieldValue;
                var tableName = fieldValue.split('.')[0];
                var fieldName = fieldValue.split('.')[1];
                if ($.isEmpty(domStorage.data(tableName))) domStorage.data(tableName, {});
                value = $.htmlDecode(domStorage.data(tableName)[fieldName]);
            }
            selectDom.val(value).trigger('change').verifyClean();
        });
    },
    reloadBusinessSelected: function(customValue, nodeDom){
        if (!$.isEmpty(customValue)){
            nodeDom.find('.component-select-other-select').findFormUnit().val(-99).trigger('change').verifyClean();
            nodeDom.find('.component-select-other .component-select-other-other input[type=text]').val(customValue).trigger('change').verifyClean();
        } else {
            nodeDom.find('.component-select-other-select').findFormUnit().val(-100).trigger('change').verifyClean();
        }
        // set ER selection to '请选择'
        $('#field-detail-er').findFormUnit().val(null).trigger('change').verifyClean();
    }
});

$.extend({
    alert: function(messageOrSettings, callback) {
        callback = callback || new Function;
        var template =
            '<div id="modal-alert" class="modal fade" data-backdrop="static">' +
            '   <div class="modal-dialog modal-sm">' +
            '       <div class="modal-content">' +
            '           <div class="modal-header">' +
            '               <button type="button" class="close" data-dismiss="modal">&times;</button>' +
            '               <h4 class="modal-title">{{title}}</h4>' +
            '           </div>' +
            '           <div class="modal-body fz16 tac">{{message}}</div>' +
            '           <div class="modal-footer">' +
            '               <button id="btn-alert-confirm" class="btn btn-primary" type="button" data-dismiss="modal">{{confirmText}}</button>' +
            '           </div>' +
            '       </div>' +
            '   </div>' +
            '</div>';
        var defaults = {
            title: '提示',
            message: '',
            confirmText: '确定',
        };
        var options = {};
        if ($.isObject(messageOrSettings)) options = $.extend(options, defaults, messageOrSettings, {
            message: $.htmlEncode($.toString(messageOrSettings.message)).replace(/\n/, '').replace(/\n/, '<br>'),
        });
        else options = $.extend(options, defaults, {
            message: $.htmlEncode($.toString(messageOrSettings)).replace(/\n/, '').replace(/\n/, '<br>'),
        });
        $('#modal-alert, .modal-backdrop').remove();
        $('body').off('click', '#btn-alert-confirm');
        $('body').off('hidden.bs.modal', '#modal-alert');
        $('body').createTemplate(template, options);
        var alertDom = $('#modal-alert');
        alertDom.modal('show');
        $('body').on('hidden.bs.modal', '#modal-alert', function() {
            if ($('.modal:visible').length > 0) $('body').addClass('modal-open');
            callback();
        });
    },
    confirm: function(messageOrSettings, confirmCallback, cancelCallback) {
        confirmCallback = confirmCallback || new Function;
        cancelCallback = cancelCallback || new Function;
        var template =
            '<div id="modal-confirm" class="modal fade" data-backdrop="static">' +
            '   <div class="modal-dialog modal-sm">' +
            '       <div class="modal-content">' +
            '           <div class="modal-header">' +
            '               <button type="button" class="close" data-dismiss="modal">&times;</button>' +
            '               <h4 class="modal-title">{{title}}</h4>' +
            '           </div>' +
            '           <div class="modal-body fz16 tac">{{message}}</div>' +
            '           <div class="modal-footer">' +
            '               <button id="btn-confirm-confirm" class="btn btn-primary" type="button" data-dismiss="modal">{{confirmText}}</button>' +
            '               <button id="btn-confirm-cancel" class="btn btn-default" type="button" data-dismiss="modal">{{cancelText}}</button>' +
            '           </div>' +
            '       </div>' +
            '   </div>' +
            '</div>';
        var defaults = {
            title: '提示',
            message: '',
            confirmText: '确定',
            cancelText: '取消',
        };
        var options = {};
        if ($.isObject(messageOrSettings)) options = $.extend(options, defaults, messageOrSettings, {
            message: $.htmlEncode($.toString(messageOrSettings.message)).replace(/\n/, '').replace(/\n/, '<br>'),
        });
        else options = $.extend(options, defaults, {
            message: $.htmlEncode($.toString(messageOrSettings)).replace(/\n/, '').replace(/\n/, '<br>'),
        });
        $('#modal-confirm, .modal-backdrop').remove();
        $('body').off('click', '#btn-confirm-confirm');
        $('body').off('hidden.bs.modal', '#modal-confirm');
        $('body').createTemplate(template, options);
        var confirmDom = $('#modal-confirm');
        confirmDom.modal('show');
        $('body').on('click', '#btn-confirm-confirm', confirmCallback);
        $('body').on('hidden.bs.modal', '#modal-confirm', function() {
            if ($('.modal:visible').length > 0) $('body').addClass('modal-open');
            cancelCallback();
        });
    },
    showMask: function() {
        $('body').showMask();
    },
    hideMask: function() {
        $('body').hideMask();
    },
    get: function(url, params, headers, others) {
        return $.ajaxApi('GET', url, params, headers, others);
    },
    post: function(url, params, headers, others) {
        return $.ajaxApi('POST', url, params, headers, others);
    },
    put: function(url, params, headers, others) {
        return $.ajaxApi('PUT', url, params, headers, others);
    },
    delete: function(url, params, headers, others) {
        return $.ajaxApi('DELETE', url, params, headers, others);
    },
    ajaxApi: function(type, url, params, headers, others) {
        params = $.extend(params, { access_token: $.getCookie('access_token') });
        headers = headers || {};
        others = others || {};
        var config = $.extend({}, others, {
            type: type,
            url: url,
            data: params,
            headers: headers,
            complete: $.hideMask,
            error: $.handleError,
            cache: false,
        });
        $.showMask()
        return $.ajax(config);
    },
    handleSuccess: function() {},
    handleError: function(XMLHttpRequest, textStatus, errorThrown, type) {
        var errorCode = XMLHttpRequest.status;
        var errorMessage = '';
        var closeAlertCallBack = '';
        for (var i = 0; i < _enumErrorMessage.length; i++)
            if (_enumErrorMessage[i].code == errorCode) {
                errorMessage = _enumErrorMessage[i].message;
                closeAlertCallBack = _enumErrorMessage[i].cb;
            }
        if (!$.isEmpty(errorMessage)) $.alert(errorMessage, closeAlertCallBack);
        else $.alert(_enumMessage.internalServerError, $.backToHome);
    },
    log: function(message) {
        if (_log) console.log(message);
    },
    getFormDetailParams: function() {
        var paramList = arguments;
        var params = {};
        for (var i = 0; i < paramList.length; i++) {
            params = $.extend(params, paramList[i]);
        }
        // delete params.form;
        // delete params.flow;
        // delete params.owner;
        delete params.approvalList;
        return params;
    },
});

$.extend({
    initPage: function(callback) {
        callback = callback || new Function;
        var employeeId = $.getCookie('userId');
        var user = window.sessionStorage.getItem('user');
        if ($.isEmpty(employeeId)) {
            window.sessionStorage.removeItem('user');
            $.login();
            return false;
        }
        _user = JSON.parse(user);
        if ($.isEmpty(user) || _user.employeeId != employeeId) {
            $.get(_apiPath + '/Apps/currentUser')
                .then(function(result) {
                    if ($.isEmpty(result)) return $.alert(_enumMessage.unauthorized, $.login);
                    for (var i = 0; i < result.roleList.length; i++)
                        result.roleList[i] = result.roleList[i].name;
                    window.sessionStorage.setItem('user', JSON.stringify(result));
                    _user = result;
                    callback();
                });
            return false;
        }
        callback();
    },
    pageStart: function(settings) {
        if (settings.title === 'admin') $('#goToFront').removeClass('dn');
        if ($.inArray("admin", _user.roleList) !== -1 && settings.title === 'eContract') $('#goToAdmin').removeClass('dn');
        $('#user-name').text(_user.chineseName);
        $('#logout').removeClass('dn');
        return new Promise(function(resolve, reject) {
            if ($.judgeAuthority(settings)) resolve();
        });
    },
    login: function() {
        $.goto(_apiPath + '/apps/login');
    },
    logoff: function() {
        $.confirm(_enumMessage.confirmLogoff, function() {
            $.post(_apiPath + '/Apps/logoff')
                .then(function(result) {
                    window.sessionStorage.removeItem('user');
                    $('#user-name').text('请登录');
                    $('#logout').addClass('dn');
                    $.alert(_enumMessage.logoffSuccess, $.login);
                });
        });
    },
    backToHome: function() {
        $.goto('/request');
    },
    judgeAuthority: function(settings) {
        if ($.isEmpty(_user)) {
            $.alert(_enumMessage.forbidden, $.login);
            return false;
        }
        var userAuthorityList = $.getUserAuthorityList(_user.roleList, settings.authorityList, settings.pageList);
        if ($.isEmpty(userAuthorityList)) {
            $.alert(_enumMessage.forbidden, $.login);
            return false;
        }
        var currentPage = $.getCurrentPage(settings.pageList);
        if (!$.judgeCurrentPageAuthority(userAuthorityList, currentPage)) {
            $.alert(_enumMessage.forbidden, $.backToHome);
            return false;
        }
        return true;
    },
    judgeCurrentPageAuthority: function(userAuthorityList, currentPage) {
        if ($.isEmpty(userAuthorityList) || $.isEmpty(currentPage)) return false;
        for (var i = 0; i < userAuthorityList.length; i++)
            if (userAuthorityList[i].id == currentPage.id) return true;
        return false;
    },
});

$.extend({
    initSubmitPage: function(params) {
        return $.get(_apiPath + '/Elancos/FormDetails/Submit/Page', params);
    },
    getSubmitList: function(params) {
        return $.get(_apiPath + '/Elancos/FormDetails/Submit', params);
    },
    initApprovalPage: function(params) {
        return $.get(_apiPath + '/Elancos/FormDetails/Approval/Page', params);
    },
    getApprovalList: function(params) {
        return $.get(_apiPath + '/Elancos/FormDetails/Approval', params);
    },
    getFormDetail: function(params) {
        var formId = $.isEmpty($.getParam('formId')) ? 1 : $.getParam('formId');
        var id = $.isEmpty($.getParam('id')) ? 0 : $.getParam('id');
        return $.get(_apiPath + '/Elancos/FormDetails/' + id, $.extend(params, { formId: formId }));
    },
    getExportList: function(params) {
        var href = _apiPath + '/ELancos/Export/FormDetails';
        if (!$.isEmpty(params)) href += '?' + $.param(params)
        //$('<a>').attr('href', href).attr('target', "_blank")[0].click();
        window.location.href = href;
    },
    getFlowDetail: function() {
        var formId = $.isEmpty($.getParam('formId')) ? 1 : $.getParam('formId');
        var id = $.isEmpty($.getParam('id')) ? 0 : $.getParam('id');
        return $.get(_apiPath + '/Elancos/FlowDetail/' + id, {
            formId: formId,
        });
    }
});

$.extend({
    getKeyValueListByIdName: function(list) {
        return $.getKeyValueList('id', 'name', list);
    },
    createDropdownList: function(type, parentId) {
        var filter = {};
        filter.type = type;
        if (!$.isEmpty(parentId)) filter.parentId = parentId;
        return $.getKeyValueListByIdName($.getFilterList(_data.dictionaryList, filter));
    },
    createApprovalList: function(approvalStep) {
        var _approvalList = [];
        var approvalList = $.getFilterList(_data.approvalList, { approvalStep: approvalStep });
        for (var i = 0; i < approvalList.length; i++) {
            var approvalItem = approvalList[i];
            _approvalList.push({ id: approvalItem.id, name: approvalItem.id + ' / ' + approvalItem.name });
        }
        return $.getKeyValueListByIdName(_approvalList);
    },
});

$.extend({
    getSubmitListMoreListTemplate: function(data) {
        var template = $('#template-more-list').html().replace(/\n/g, '').replace(/\r/g, '');
        return Template7.compile(template)(data);
    },
    //历史遗留问题，econtract表单没有request date
    getRequestDate: function(data) {
        if ($.isEmpty(data.requestDate) && $.isEmpty(data.createdAt)) return '';
        if ($.isEmpty(data.requestDate)) return $.toDate(data.createdAt);
        return $.toDate(data.requestDate);
    },
    toFormStatus: function(formDetail) {
        status = formDetail.formStatus;
        if ($.isEmpty(status)) return '';
        if(status == -4 && formDetail.flowId == 2) return '已完成';
        if (status == _enumFormStatus.saved) return '草稿';
        if (status == _enumFormStatus.completed) return '已完成';
        if (status < _enumFormStatus.submitted) return '已拒绝';
        return '审核中';
    },
    getApproval: function(formDetail, index) {
        index = $.isEmpty(index) ? 0 : index;
        var message = '--';
        var approvalList = formDetail.approvalList;
        var formStatus = formDetail.formStatus;
        var approvalFinalStep = formDetail.approvalFinalStep;
        var approvalStep = formStatus < _enumFormStatus.submitted ? formDetail.approvalStep - 1 : formDetail.approvalStep;
        var targetApprovalStep = 0;
        var approvalStepIndex = 0;
        var targetApprovalStepIndex = 0;
        var approvalStepList = [];
        for (var a = 0; a < approvalList.length; a++) {
            var approvalItem = approvalList[a];
            if ($.isEmpty(approvalStepList)) {
                approvalStepList.push(approvalItem.approvalStep);
            } else {
                var isExist = false;
                for (var b = 0; b < approvalStepList.length; b++) {
                    if (approvalItem.approvalStep == approvalStepList[b]) {
                        isExist = true;
                    }
                }
                if (!isExist) {
                    approvalStepList.push(approvalItem.approvalStep);
                }
            }
        }
        for (var i = 0; i < approvalStepList.length; i++) {
            var approvalStepItem = approvalStepList[i];
            if (approvalStepItem == approvalStep) {
                approvalStepIndex = i;
            }
        }
        targetApprovalStepIndex = approvalStepIndex + index;
        targetApprovalStep = approvalStepList[targetApprovalStepIndex];
        targetApprovalStep = $.isEmpty(targetApprovalStep) ? -1 : targetApprovalStep;
        if (formStatus == _enumFormStatus.saved || $.isEmpty(approvalList) ||
            formStatus < _enumFormStatus.submitted && targetApprovalStep > approvalStep ||
            targetApprovalStep <= 0 || targetApprovalStep > approvalFinalStep) {
            return message;
        }
        var targetApprovalList = $.getFilterList(approvalList, { approvalStep: targetApprovalStep });
        for (var i = 0; i < targetApprovalList.length; i++) {
            var targetApprovalItem = targetApprovalList[i];
            if (formStatus == _enumFormStatus.completed) {
                if (targetApprovalItem.approvalStatus != _enumApprovalStatus.待审批) {
                    message += ',' + targetApprovalItem.approverEmployeeId + ' / ' + targetApprovalItem.approver.chineseName;
                }
            } else if (formStatus >= _enumFormStatus.submitted) {
                if (targetApprovalItem.approvalStep >= approvalStep) {
                    message += ',' + targetApprovalItem.approverEmployeeId + ' / ' + targetApprovalItem.approver.chineseName;
                } else {
                    if (targetApprovalItem.approvalStatus != _enumApprovalStatus.待审批) {
                        message += ',' + targetApprovalItem.approverEmployeeId + ' / ' + targetApprovalItem.approver.chineseName;
                    }
                }
            } else {
                if (targetApprovalItem.approvalStep > approvalStep) {
                    message += ',' + targetApprovalItem.approverEmployeeId + ' / ' + targetApprovalItem.approver.chineseName;
                } else {
                    if (targetApprovalItem.approvalStatus != _enumApprovalStatus.待审批) {
                        message += ',' + targetApprovalItem.approverEmployeeId + ' / ' + targetApprovalItem.approver.chineseName;
                    }
                }
            }
        }
        return message.replace(/--,/, '');
    },
    toApprovalStatus: function(data) {
        var currentUserEmployeeId = _user.employeeId;
        var formStatus = data.formStatus;
        var approvalList = data.approvalList;
        var approvalStep = data.approvalStep;
        var approvalStatus = _enumApprovalStatus.待审批;
        var approvalFinalStep = data.approvalFinalStep;
        for (var a = 0; a < approvalList.length; a++) {
            var approvalItem = approvalList[a];
            if (approvalItem.approverEmployeeId == currentUserEmployeeId &&
                approvalItem.approvalStep == approvalStep) {
                approvalStatus = approvalItem.approvalStatus;
            }
            if(approvalItem.flowId == 2 && approvalItem.approvalStep == approvalStep && approvalStatus != _enumApprovalStatus.已通过) {
                approvalStatus = approvalItem.approvalStatus;
            }
            if (approvalItem.approverEmployeeId === currentUserEmployeeId &&
                approvalItem.approvalStep < approvalStep) {
                if (approvalItem.approvalItem != _enumApprovalStatus.待审批) {
                    for (var b = 0; b < approvalList.length; b++) {
                        if (approvalList[b].approvalStep == approvalItem.approvalStep &&
                            approvalList[b].approvalStatus != _enumApprovalStatus.待审批) {
                            approvalStatus = approvalList[b].approvalStatus;
                        }
                    }
                } else {
                    approvalStatus = approvalItem.approvalStatus;
                }
            }
        }
        if(formStatus == -4 && data.flowId == 2) return '已通过';
        if (approvalStatus == _enumApprovalStatus.待审批) return '待审批';
        if (approvalStatus == _enumApprovalStatus.已通过) return '已通过';
        if (approvalStatus == _enumApprovalStatus.已拒绝) return '已拒绝';
        return '';
    },
    toApprovalStatusInDetail: function(status) {
        if (status == 0) return '待审批'
        if (status > 0) return '已通过';
        if (status < 0) return '已拒绝';
    },
    getStatusClass: function(data) {
        var status = $.toApprovalStatus(data);
        if (status == '待审批') return 'list-pending'
        if (status == '已通过') return 'list-approval';
        if (status == '已拒绝') return 'list-reject';
    },
    getSubmitStatusClass: function(data) {
        formStatus = data.formStatus;
        if (formStatus === -99) return '';
        if(formStatus == -4 && data.flowId == 2) return 'list-approval';
        if (formStatus === 99) return 'list-approval';
        if (formStatus < 0) return 'list-reject';
        return 'list-pending';
    },
    getFlowDetailClass: function(status) {
        if (status == 0) return 'list-pending'
        if (status > 0) return 'list-approval';
        if (status < 0) return 'list-reject';
    },
    hasEditBtn: function(formDetail) {
        var formId = formDetail.formId;
        var formStatus = formDetail.formStatus;
        if (formId == 1) {
            if (formStatus == _enumFormStatus.saved ||
                formStatus < _enumFormStatus.submitted) {
                return '';
            }
        }
        return 'dn';
    },
    hasFlowBtn: function(formStatus) {
        if (formStatus != _enumFormStatus.saved) return '';
        return 'dn';
    },
    hasDeleteBtn: function(formDetail) {
        var formId = formDetail.formId;
        var formStatus = formDetail.formStatus;
        if (formId == 1 || formId == 3) {
            if (formStatus == _enumFormStatus.saved) {
                return '';
            }
        }
        return 'dn';
    },
    hasCancelBtn: function(formDetail) {
        var formId = formDetail.formId;
        var formStatus = formDetail.formStatus;
        if (formId == 1) {
            if (formStatus == _enumFormStatus.saved ||
                formStatus < _enumFormStatus.submitted) {
                return 'dn';
            }
        } 
        if (formId == 2 && formDetail.formStatus != 0) {
            return 'dn';
        }
        if( formId == 3 ){
            if(formStatus != _enumFormStatus.submitted) return 'dn';
        }
        return '';
    },
    getApprovalStatus: function(status) {
        var statusText = "待审批";
        if (status < 0) {
            statusText = "拒绝";
        }
        if (status > 0) {
            statusText = "同意";
        }
        return statusText;
    },
    getApprovalStatusEN: function(status) {
        var statusText = "pending";
        if (status < 0) {
            statusText = "reject";
        }
        if (status > 0) {
            statusText = "approval";
        }
        return statusText;
    },
    getApprovalStatusClass: function(status) {
        var statusText = "list-pending";
        if (status < 0) {
            statusText = "list-reject";
        }
        if (status > 0) {
            statusText = "list-approval";
        }
        return statusText;
    },
    getFormDetailUrl: function(formId) {
        switch (formId) {
            case 1:
                return 'eContractDetail';
                break;
            case 2:
                return 'manpowerRequisitionDetail';
                break;
            case 3: 
                return 'sourcingRequisitionDetail';
                break;
            default:
                break;
        }
    },
    getUser: function(user) {
        if ($.isEmpty(user)) return '';
        return user.employeeId + ' / ' + user.chineseName;
    },
    exitForm: function() {
        var backId = $.getParam('backId');
        if ($.isEmpty(backId)) {
            backId = 2;
        }
        var listConfig = $.getPageConfig(backId, _eLancoSettings.pageList);
        $.goto(listConfig.href);
    },
    getCurrentPageId: function() {
        return $.getCurrentPage(_eLancoSettings.pageList).id;
    }
});

$.extend({
    createSelect: function() {
        var mySelect = document.createElement("mySelect");
        mySelect.id = "mySelect";
        document.body.appendChild(mySelect);

        var obj = document.getElementById('mySelect');

        if (obj.children.length == 0) {
            var opt = document.createElement('option');
            opt.id = Boolean(true);
            opt.innerHTML = true;
            obj.appendChild(opt);

            var option = document.createElement('option');
            option.id = Boolean(false);
            option.innerHTML = false;
            obj.appendChild(option);
        }
        
        return $.getKeyValueList('id', 'innerHTML', obj.childNodes);
    },
});